import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/8 16:09
 * @description
 */
public class Solution {
    public static void main(String[] args) {

    }
    public String replaceWords(List<String> dictionary, String sentence) {
        dictionary.sort((a, b) -> {
            return a.length() == b.length() ? a.compareTo(b) : a.length() - b.length();
        });
        String[] se = sentence.split(" ");
        for (int i = 0; i < se.length; i++) {
            String temp = se[i];
            for (String prefix : dictionary) {
                if (prefix.length() > temp.length()) {
                    break;
                }
                if (temp.startsWith(prefix)) {
                    se[i] = prefix;
                    break;
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        for (String s: se) {
            sb.append(s + " ");
        }
        return sb.substring(0, sb.length() - 1);
    }
}
